/*
 * $RCSfile: TablespaceAction.java,v $
 * $Revision: 1.1 $
 * $Date: 2017-05-17 $
 *
 * Copyright (C) 2008 Skin, Inc. All rights reserved.
 *
 * This software is the proprietary information of Skin, Inc.
 * Use is subject to license terms.
 */
package com.skin.webcat.action.oracle;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.skin.j2ee.action.BaseAction;
import com.skin.j2ee.annotation.UrlPattern;
import com.skin.j2ee.util.JsonUtil;
import com.skin.webcat.database.oracle.DataFile;
import com.skin.webcat.database.oracle.Oracle;
import com.skin.webcat.database.oracle.OracleURL;
import com.skin.webcat.database.oracle.Tablespace;
import com.skin.webcat.util.Jdbc;
import com.skin.webcat.util.Webcat;

/**
 * <p>Title: TablespaceAction</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author xuesong.net
 * @version 1.0
 */
public class TablespaceAction extends BaseAction {
    private static final Logger logger = LoggerFactory.getLogger(TablespaceAction.class);

    /**
     * @throws ServletException
     * @throws IOException
     */
    @UrlPattern("/webcat/oracle/tablespace.html")
    public void tablespace() throws ServletException, IOException {
        String name = this.getTrimString("name");
        OracleURL oracleURL = null;

        if(name.length() > 0) {
            Connection connection = null;

            try {
                connection = Webcat.getConnection(name);
                oracleURL = Oracle.getURL(connection);
            }
            catch(Exception e) {
                logger.error(e.getMessage(), e);
            }
            finally {
                Jdbc.close(connection);
            }
        }

        /**
         * 使用示例URL
         */
        if(oracleURL == null) {
            oracleURL = OracleURL.parse("jdbc:oracle:thin:@192.168.1.1:1521:ORCL");
        }

        this.setAttribute("name", name);
        this.setAttribute("oracleURL", oracleURL);
        this.forward("/template/webcat/oracle/tablespace.jsp");
    }

    /**
     * @throws ServletException
     * @throws IOException
     */
    @UrlPattern("/webcat/oracle/tablespace/getCreateSql.html")
    public void getCreateSql() throws ServletException, IOException {
        String sql = this.getCreateTablespaceSql();
        JsonUtil.success(this.request, this.response, sql);
    }

    /**
     * @throws ServletException
     * @throws IOException
     */
    @UrlPattern("/webcat/oracle/tablespace/create.html")
    public void create() throws ServletException, IOException {
        String sql = this.getCreateTablespaceSql();
        JsonUtil.success(this.request, this.response, sql);
    }

    /**
     * @return String
     */
    private String getCreateTablespaceSql() {
        Tablespace tablespace = this.getTablespace();
        String tablespaceType = tablespace.getTablespaceType();
        StringBuilder buffer = new StringBuilder();
        buffer.append("-- create tablespace\r\n");

        if("temporary".equals(tablespaceType)) {
            buffer.append("create temporary tablespace ${tablespaceName} tempfile\r\n");
        }
        else if("undo".equals(tablespaceType)) {
            buffer.append("create undo tablespace ${tablespaceName} datafile\r\n");
        }
        else {
            buffer.append("create tablespace ${tablespaceName} datafile\r\n");
        }

        List<DataFile> dataFileList = tablespace.getDataFiles();

        if(dataFileList != null && dataFileList.size() > 0) {
            for(DataFile dataFile : dataFileList) {
                buffer.append("'");
                buffer.append(dataFile.getPath());
                buffer.append("'");
                buffer.append(" size ");
                buffer.append(dataFile.getInitSize());
                buffer.append("M");

                if(dataFile.getReuse()) {
                    buffer.append(" reuse");
                }

                if(dataFile.getAutoExtend()) {
                    buffer.append("autoextend on next ${nextExtend}M");
                }

                if(dataFile.getUnlimited()) {
                    buffer.append(" maxsize unlimited");
                }
                else {
                    buffer.append(" maxsize ${maxSize}M");
                }
                buffer.append("\r\n");
            }
            buffer.append(";");
        }
        buffer.append("\r\n\r\n\r\n\r\n");
        return buffer.toString();
    }

    /**
     * @return Tablespace
     */
    private Tablespace getTablespace() {
        Tablespace tablespace = new Tablespace();
        tablespace.setName(this.getTrimString("tableSpaceName"));
        tablespace.setTablespaceType(this.getTrimString("tableSpaceType"));
        /*
        tablespace.setDataFile(this.getTrimString("dataFile"));
        tablespace.setInitSize(this.getInteger("initSize", 1024));
        tablespace.setAutoExtend(this.getBoolean("autoExtend"));
        tablespace.setNextExtend(this.getInteger("nextExtend", 2048));
        tablespace.setMaxSize(this.getInteger("maxSize", 20480));
        tablespace.setManagement(this.getTrimString("management"));
        */
        return tablespace;
    }
}
